-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tag Processor: Add ability to stop at tag closers, if requested #45789
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
@@ -408,7 +439,16 @@ public function next_tag( $query = null ) { | |||
return false; | |||
} | |||
|
|||
$this->parse_tag_opener_attributes(); | |||
while ( $this->parse_next_attribute() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the inlining
* Example: | ||
* <code> | ||
* $p = new WP_HTML_Tag_Processor( '<div></div>' ); | ||
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | ||
* $p->is_tag_closer() === false; | ||
* | ||
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | ||
* $p->iss_tag_closer() === true; | ||
* </code> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice example! A minor typo:
* Example: | |
* <code> | |
* $p = new WP_HTML_Tag_Processor( '<div></div>' ); | |
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | |
* $p->is_tag_closer() === false; | |
* | |
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | |
* $p->iss_tag_closer() === true; | |
* </code> | |
* Example: | |
* <code> | |
* $p = new WP_HTML_Tag_Processor( '<div></div>' ); | |
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | |
* $p->is_tag_closer() === false; | |
* | |
* $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | |
* $p->is_tag_closer() === true; | |
* </code> |
$p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ); | ||
$this->assertFalse( $p->is_tag_closer(), 'Indicated a tag opener is a tag closer' ); | ||
$this->assertTrue( $p->next_tag( [ 'tag_name' => 'div', 'tag_closers' => 'visit' ] ), 'Did not stop at desired tag closer' ); | ||
$this->assertTrue( $p->is_tag_closer(), 'Indicated a tag closer is a tag opener' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see some tests to confirm the behavior of the other methods in the tag closer state, e.g. update_attribute()
, __toString()
, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a test to confirm that ops are inert inside closed tags
This looks good @dmsnell! There are some conflicts and I left a few comments, but the direction is great. |
46e906e
to
1652965
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the updates @dmsnell! The linter fails, but the tests are passing. This is a solid PR that makes a great foundation for the upcoming unsafe mode. 🚢
5e96d70
to
8bc31a3
Compare
Fixed the lint issues |
8bc31a3
to
0d78f1d
Compare
`WP_HTML_Tag_Processor` introduces the ability to walk through an HTML document and modify attributes on tag openers. At times it could be useful to stop also at tag closers in order to perform augmented inspection and querying of a document. In this patch we're opening up a mode that allows just that; if querying with the `[ "tag_closers" => "visit" ]` query parrameter then in addition to stopping at tag openers, this will allow stopping at all tag tokens regardless of open or close status.
0d78f1d
to
7e76592
Compare
Notes
What?
Add an option to the
WP_HTML_Tag_Processor
query to indicate that the processor should stop and visit tag closers while walking through the input document.Why?
WP_HTML_Tag_Processor
introduces the ability to walk through an HTML document and modify attributes on tag openers.At times it could be useful to stop also at tag closers in order to perform augmented inspection and querying of a document.
In this patch we're opening up a mode that allows just that; if querying with the
[ "tag_closers" => "visit" ]
query parrameter then in addition to stopping at tag openers, this will allow stopping at all tag tokens regardless of open or close status.How?
Introduces internal mechanics to detect and stop at tag closers.
Exposes new query option
"tag_closers" => "visit" | "skip"
to pass intonext_tag()
which indicates whether or not to stop at tag closers.Exposes new
is_tag_closer()
method to determine if current tag is an opener or closer (from calling code).Testing Instructions
Review the new unit tests to confirm behavior.